package bj.gui

//import bj.actor.BJTable

import scala.swing.event
import event.ButtonClicked
import scala.swing.SimpleGUIApplication
import scala.swing.Frame
import scala.swing.Label
import scala.swing.Swing
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.GridPanel
import scala.swing.BorderPanel
import scala.swing.Component
import javax.swing.SpinnerNumberModel
import javax.swing.JSpinner
import javax.swing.JTextField
import scala.swing.TextField
import scala.swing.BoxPanel
import scala.swing.Orientation
import scala.swing.MainFrame
import javax.swing.BoxLayout
import java.awt.FlowLayout
import scala.swing.Table
import scala.swing.ScrollPane
import scala.swing.MenuBar
import scala.swing.Menu
import scala.swing.RadioMenuItem
import scala.swing.ButtonGroup
import scala.swing.MenuItem
import scala.swing.Action
import scala.swing.Separator
import scala.swing.CheckMenuItem
import java.awt.Dimension
import scala.swing.event.TableRowsSelected
import scala.swing.event.TableRowsSelected
import javax.swing.ImageIcon
import bj.actor.Send
import bj.actor.Receive
import bj.actor.Intermediator
import bj.actor.House
import bj.actor.SendMsg
import bj.actor.GetPlayers
import bj.actor.SendPlayers
import bj.util.Logs
import org.apache.log4j.PropertyConfigurator
import javax.swing.table.DefaultTableModel

class Players extends Frame with Logs {
  javax.swing.UIManager.setLookAndFeel(Constants.LF)
  PropertyConfigurator.configure("log4j.properties")

  title = "Blackjack - Players View"
  minimumSize = Constants.lobbySize

  // Listen to the house so I can process events received from it
  implicit val intermediator = Intermediator(House)

  val historyButton = new Button {
    text = "Player History"
    enabled = false
  }
  val refreshButton = new Button { text = "Refresh" }
  val closeButton = new Button { text = "Close" }

  var tableData: Array[Array[AnyRef]] = new Array(0);

  val headers = Array("Name", "Table", "Bankroll", "Human/Bot")

  val tableModel = new DefaultTableModel(
    tableData.asInstanceOf[Array[Array[Object]]], // Data
    headers.asInstanceOf[Array[Object]]) // Column names
  val playersTable = new Table(tableData.size, headers.size) {
    model = tableModel
    showGrid = true
    gridColor = Constants.gridColor
    border = Swing.EmptyBorder(20)
    visible = true
  }

  val midPanel = new FlowPanel() {
    contents +=
      new ScrollPane {
        viewportView = playersTable
        preferredSize = new Dimension(600, 110)
      }
    background = Constants.greenBack
  }

  val buttonPanel = new FlowPanel() {
    contents += new Label {
      icon = new ImageIcon("images/BJLogo2.gif")
    }
    contents += historyButton
    contents += refreshButton
    contents += closeButton
    background = Constants.greenBack
  }
  contents = new BorderPanel() {

    add(new Label { text = "Players" }, BorderPanel.Position.North)
    add(midPanel, BorderPanel.Position.Center)
    add(buttonPanel, BorderPanel.Position.South)
    border = Swing.EmptyBorder(10)
    background = Constants.greenBack
  }

  listenTo(intermediator)
  listenTo(historyButton)
  listenTo(closeButton)
  listenTo(refreshButton)
  listenTo(playersTable.selection)
  reactions += {
    case ButtonClicked(`historyButton`) =>
      println("Clicked history button with array element " + playersTable.selection.rows + " selected")
      if (playersTable.selection.rows.size > 1)
        new BjInfoBox(new Label("Please select only one player for this function"))
      else
        new PlayerHistoryBox()
    case ButtonClicked(`closeButton`) =>
      println("Clicked close button")
      this.close()
    case ButtonClicked(`refreshButton`) =>
      println("Clicked Refresh button")
      Send(GetPlayers)
      playersTable.selection.rows.clear
      historyButton.enabled = false
    case TableRowsSelected(`playersTable`, range, adjusting) =>
      historyButton.enabled = true
    // Receive a random message from the house; display it in a pop up window
    case Receive(SendMsg(message)) => new BjInfoBox(new Label { text = message })
    // Receive a SendPlayers case from the house. Use the data to refresh the players table in the GUI
    case Receive(SendPlayers(players)) => refreshTable(players)
  }
  visible = true;
  // Request the current Players data from the House
  Send(GetPlayers)

  /**
   * Refresh the data currently in the table with new data just received from the House
   */
  def refreshTable(value: Array[Array[AnyRef]]): Unit = {
    println("Refreshing player Table data")
    tableData.foreach(t => tableModel.removeRow(0))
    tableData = value
    tableData.foreach(t => tableModel.addRow(t))
  }
}